<template>
  <div class="choose" @click="toBuy(false)" v-if="goodsInfo">
    <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '70%' }"
      @click="toBuy(false)"
    >
      <div class="goods">
        <div class="msg">
          <div class="left">
            <img
              :src="goodsInfo.defaultProduct.goodsPics[0]"
              alt=""
              @click.stop="showPreview(goodsInfo.defaultProduct.goodsPics[0])"
            />
            <div class="type">
              <p>￥{{ goodsInfo.defaultProduct.productPrice }}</p>
              <p>已选规格：{{ goodsInfo.defaultProduct?.getSpecValues }}</p>
            </div>
          </div>
          <div class="right">
            <img
              @click.stop="toBuy(false)"
              src="	https://image-mall.gxptkc.com/m/resources/goods_detail/close.png"
              alt=""
            />
          </div>
        </div>
        <div class="number-type">
          <p>选择净含量</p>
          <ul class="list">
            <li
              v-for="i in goodsInfo.specs[0]?.specValueList"
              :key="i.specValueId"
              :class="{
                active:
                  i.specValueId == goodsInfo.defaultProduct?.getSpecValueIds,
              }"
              @click.stop="chooseType(goodsInfo.goodsId, i.specValueId)"
            >
              {{ i.specValue }}
            </li>
          </ul>
        </div>
        <div class="number" @click.stop="">
          <p>购买数量</p>
          <div class="counter">
            <span @click.stop="count--">-</span>
            <input type="text" v-model="count" />
            <span @click.stop="count++">+</span>
          </div>
        </div>
      </div>
      <div class="action">
        <button
          @click="addToCart(goodsInfo.defaultProduct.productId)"
          v-if="addOrBuy == 1"
          :class="{ color: addOrBuy == 1 }"
        >
          加入购物车
        </button>
        <button @click="toConfirm(goodsInfo.defaultProduct.productId)" v-else>
          立即购买
        </button>
      </div>
    </van-popup>
  </div>
</template>

<script>
import qs from "qs";
import { mapState, mapMutations } from "vuex";
import Vue from "vue";
import { Toast } from "vant";
import { ImagePreview } from "vant";
Vue.use(Toast);
export default {
  created() {
    this.goodsInfo = this.goodsProduct;
    this.token = localStorage.getItem("fastCar-token");
  },
  props: ["goodsProduct", "addOrBuy"],
  data() {
    return {
      show: false,
      count: 1,
      goodsInfo: null,
      token: null,
    };
  },
  computed: {
    ...mapState(["showBuy"]),
  },
  methods: {
    ...mapMutations(["toBuy"]),

    showPreview(img) {
      ImagePreview([img]);
    },
    async chooseType(goodsId, typeId) {
      let res = await this.axios({
        method: "GET",
        url: `https://m-mall.gxptkc.com/v3/goods/front/goods/productInfo?goodsId=${goodsId}&specValueIds=${typeId}`,
        headers: {
          authorization: `Bearer ${this.token}`,
        },
      });
      this.goodsInfo = res.data.data;
    },
    toConfirm(productId) {
      this.$router.push(`/confirm?productId=${productId}&source=${this.count}`);
    },
    async addToCart(id) {
      let res = await this.axios({
        url: "https://m-mall.gxptkc.com/v3/business/front/cart/add",
        method: "POST",
        headers: {
          authorization: `Bearer ${this.token}`,
          "content-type": "application/x-www-form-urlencoded",
        },
        data: qs.stringify({
          productId: id,
          number: this.count,
        }),
      });
      Toast(res.data.msg);
    },
  },
  watch: {
    showBuy(newVal) {
      this.show = newVal;
    },
  },
};
</script>
<style lang="scss" scoped>
.choose {
  .goods {
    padding: 10rem;
    .msg {
      display: flex;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        img {
          width: 75rem;
          height: 75rem;
          margin-right: 15rem;
        }
        .type {
          p {
            font-size: 25rem;
            color: #fc1c1c;
            &:last-child {
              font-size: 14rem;
              color: #343434;
              margin-top: 6rem;
              box-sizing: border-box;
            }
          }
        }
      }
      .right {
        width: 23rem;
        height: 23rem;
      }
    }
    .number-type {
      p {
        font-size: 14rem;
        color: #666;
        margin: 15rem 0 5rem;
      }
      ul.list {
        padding: 10rem;
        display: flex;
        justify-content: left;
        align-items: center;
        flex-wrap: wrap;
        li {
          padding: 8rem 12rem;
          background-color: #eee;
          border-radius: 30rem;
          font-size: 13rem;
          color: #343434;
          margin: 0 10rem 10rem 0;
          &.active {
            color: #fc1c1c;
            border: 1rem solid #fc1c1c;
            background-color: #fff;
          }
        }
      }
    }
    .number {
      font-size: 14rem;
      color: #666;
      display: flex;
      justify-content: space-between;
      margin-top: 10rem;
      p {
      }
      .counter {
        height: 25rem;
        display: flex;
        color: #999;
        font-weight: bold;
        span {
          padding: 3rem 3rem;
          border: 1rem solid rgb(245, 245, 245);
          display: flex;
          align-items: center;
          justify-content: center;
          width: 25rem;
        }
        input {
          width: 39rem;
          padding: 0;
          text-align: center;
          border: 1rem solid rgb(245, 245, 245);
        }
      }
    }
  }
  .action {
    height: 50rem;
    width: 100%;
    display: flex;
    justify-content: center;
    border-top: 1px solid rgb(245, 245, 245);
    align-items: center;
    position: fixed;
    bottom: 0;
    button {
      height: 35rem;
      width: 80%;
      border: none;
      font-size: 15rem;
      color: #fff;

      background: #f30300;
      border-radius: 17rem;
      &.color {
        background: #ff821c;
      }
    }
  }
}
</style>